/**
 * @author Phuc "Noel" Vo
 */
package com.example.noelwhereismystuffs.views.SignInActivity;

import com.example.noelproject.R;
import com.example.noelwhereismystuffs.models.Database;
import com.example.noelwhereismystuffs.models.SignInModel;
import com.example.noelwhereismystuffs.models.User;
import com.example.noelwhereismystuffs.presenters.SignInPresenter;
import com.example.noelwhereismystuffs.views.AdminProfileUIActivity.AdminProfileUIActivity;
import com.example.noelwhereismystuffs.views.RegisterActivity.RegisterActivity;
import com.example.noelwhereismystuffs.views.UserProfileActivity.UserProfileActivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class SignInActivity extends Activity implements ISignInView {
	private SignInPresenter myPresenter;
	private static User myUser = User.NULL_USER;
	private static final String errorMessage = "You have entered wrong email or password.";
	private static final String infoError = "Please enter your email and password.";
	private static final String lockEmailMessage = "Your account has been locked.";
	private static Database myData;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		myData = (Database) getIntent().getSerializableExtra("Database");
		if (myData != null){
			Log.d("SignInActivity","get data");
		}else{
			myData = new Database();
			Log.d("SignInActivity","no data");
		}
		myPresenter = new SignInPresenter(this, new SignInModel(myData));
		setContentView(R.layout.activity_sign_in);
	}

	/**
	 * Handle SignIn button 
	 * @param source
	 */
	public void onSignInClick(View source) {
		String email = getEmailField().getText().toString();
		String password = getPasswordField().getText().toString();
		if (email.equals("") || password.equals("")) {
			getEmailField().requestFocus();
			getEmailField().setError(infoError);
		} else {
			myPresenter.onSignInClick(email, password);
		}
	}

	/**
	 * Handle Register button
	 * @param source
	 */
	public void onRegisterClick(View source) {
		myPresenter.onRegisterClick();
	}

	/**
	 * Get information from Password Field
	 * @return
	 */
	private EditText getPasswordField() {
		return (EditText) findViewById(R.id.PasswordEditText);
	}

	/**
	 * Get information from Email Field
	 * @return
	 */
	private EditText getEmailField() {
		return (EditText) findViewById(R.id.EmailEditText);
	}

	@Override
	public void advanceLogin() {
		// create an intent (a request for phone OS to do something)
		Intent intent = new Intent(this, UserProfileActivity.class);
		intent.putExtra("User", myUser);
		intent.putExtra("Database", myData);
		// now pass that to the phone OS and ask to start it
		startActivity(intent);
	}

	@Override
	public void advanceFalse() {
		getEmailField().requestFocus();
		getEmailField().setError(errorMessage);
	}

	@Override
	public void advanceRegister() {
		// create an intent (a request for phone OS to do something)
		Intent intent = new Intent(this, RegisterActivity.class);
		intent.putExtra("Database", myData);
		// now pass that to the phone OS and ask to start it
		startActivity(intent);

	}

	@Override
	public void advanceLockEmail() {
		getEmailField().requestFocus();
		getEmailField().setError(lockEmailMessage);
	}

	@Override
	public void setUser(User u) {
		myUser = u;
	}

	@Override
	public void advanceAdminLogin() {
		// create an intent (a request for phone OS to do something)
		Intent intent = new Intent(this, AdminProfileUIActivity.class);
		intent.putExtra("User", myUser);
		intent.putExtra("Database", myData);
		// now pass that to the phone OS and ask to start it
		startActivity(intent);
		
	}

	@Override
	public void advanceNoAccount() {
		getEmailField().requestFocus();
		getEmailField().setError("The account doesn't exists");
		
	}

}
